package com.abbcc.module.admin;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.sf.json.JSONArray;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;
import org.joda.time.DateTime;
import org.springframework.beans.factory.annotation.Autowired;

import com.abbcc.action.BaseAction;
import com.abbcc.common.PaginationSupport;
import com.abbcc.models.TdProduct;
import com.abbcc.service.ProductService;
import com.abbcc.util.DateUtil;

public class ProductStatAction extends BaseAction<TdProduct>{
	@Autowired
	ProductService productService;
	public String show(){
		return JSON;
	}
	public String productIndex(){
		deposit("allProduct", getProCountByMarked("all").getTotalCount());
		deposit("markedProduct",getProCountByMarked("marked").getTotalCount());
		deposit("todayProduct",getProCountByMarked("today").getTotalCount());
		deposit("todayDeliveryProduct",getProCountByMarked("todayDeliveryProduct").getTotalCount());
		deposit("threeDeliveryProduct",getProCountByMarked("threeDeliveryProduct").getTotalCount());
		return "index";
	}
	public String stat(){
		DetachedCriteria dc = DetachedCriteria.forClass(entityClass);
		List<TdProduct> list = baseService.findAllByCriteria(dc);
		DateTime dt = new DateTime(new Date());
		int thisYear = dt.getYear();
		Map mapthisyear = new HashMap();
		Map maplastyear = new HashMap();
		Map mapbeforlastyear = new HashMap();
		int[] thisyear1 = new int[12];
		int[] lastyear1 = new int[12];
		int[] beforlastyear1 = new int[12];
		for (TdProduct au : list) {
			DateTime dtt = new DateTime(au.getAddTime());
			if (dtt.getYear() == thisYear) {
				if (thisyear1[dtt.getMonthOfYear() - 1] == 0) {
					thisyear1[dtt.getMonthOfYear() - 1] = au.getProCount();
				} else {
					thisyear1[dtt.getMonthOfYear() - 1] = thisyear1[dtt
							.getMonthOfYear() - 1] + au.getProCount();
				}
			}
			if (dtt.getYear() == thisYear - 1) {
				if (lastyear1[dtt.getMonthOfYear() - 1] == 0) {
					lastyear1[dtt.getMonthOfYear() - 1] = au.getProCount();
				} else {
					lastyear1[dtt.getMonthOfYear() - 1] = lastyear1[dtt
							.getMonthOfYear() - 1] + au.getProCount();
				}
			}
			if (dtt.getYear() == thisYear - 2) {
				if (beforlastyear1[dtt.getMonthOfYear() - 1] == 0) {
					beforlastyear1[dtt.getMonthOfYear() - 1] = au.getProCount();
				} else {
					beforlastyear1[dtt.getMonthOfYear() - 1] = beforlastyear1[dtt
							.getMonthOfYear() - 1] + au.getProCount();
				}
			}
		}
		mapthisyear.put("name", thisYear + "年");
		mapthisyear.put("data", thisyear1);

		maplastyear.put("name", thisYear - 1 + "年");
		maplastyear.put("data", lastyear1);

		mapbeforlastyear.put("name", thisYear - 2 + "年");
		mapbeforlastyear.put("data", beforlastyear1);

		Map[] m = { mapthisyear, maplastyear, mapbeforlastyear };
		JSONArray json = JSONArray.fromObject(m);
		result = json.toString();
		return JSON;
	}
	public PaginationSupport getProCountByMarked(String m){
		DetachedCriteria dc = DetachedCriteria.forClass(entityClass);
		if(m.equals("marked"))
			dc.add(Restrictions.eq("marked", "1"));
		if(m.equals("today"))
			dc.add(Restrictions.between("addTime", DateUtil.getDateStart(new Date()),new Date()));
		if(m.equals("todayDeliveryProduct"))
			dc.add(Restrictions.between("deliveryTime", DateUtil.getDateStart(new Date()),new Date()));
		if(m.equals("threeDeliveryProduct"))
			dc.add(Restrictions.between("deliveryTime", DateUtil.getDateEnd(new Date()),DateUtil.getDateAfter(new Date(), 3)));
		dc.add(Restrictions.ne("state", "3")).setProjection(Projections.count("productId"));
		pageList = productService.findPageByCriteria(dc, pageSize, startIndex);
		return pageList;
	}
}
